"use strict";
cc._RF.push(module, '6470bsARL9FvYTZPAHELnaV', 'ScrollViewAssit');
// script/standar/utils/scrollView/ScrollViewAssit.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var ScrollViewEvent_1 = require("./ScrollViewEvent");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
/**
 * @classdesc 只渲染可视区域的ScrollView
 * @author caizhitao
 * @version 0.1.0
 * @since 2019-07-12
 * @description
 *
 * 用法：
 *
 *      1. 将本组件挂载在节点上即可，和正常ScrollView使用一致
 *
 * 原理：
 *
 *      1. 滚动时，判断子节点是否进入了/离开了可视区域
 *      2. 根据结果回调对应事件，在可以实现类似以下功能：
 *          * 控制可视区域Item显示（透明度改为 255 ），非可视区域Item隐藏（透明度改为 0 ）
 */
var ScrollViewAssit = /** @class */ (function (_super) {
    __extends(ScrollViewAssit, _super);
    function ScrollViewAssit() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.scrollView = null;
        _this.caculatePosition = false;
        return _this;
    }
    ScrollViewAssit_1 = ScrollViewAssit;
    ScrollViewAssit.prototype.onLoad = function () {
        this.scrollView = this.node.getComponent(cc.ScrollView);
    };
    ScrollViewAssit.prototype.onEnable = function () {
        this.scrollView.node.on("scrolling", this._onScrollingDrawCallOpt, this);
    };
    ScrollViewAssit.prototype.onDisable = function () {
        this.scrollView.node.off("scrolling", this._onScrollingDrawCallOpt, this);
    };
    ScrollViewAssit.prototype._onScrollingDrawCallOpt = function () {
        if (this.scrollView.content.childrenCount == 0) {
            return;
        }
        this.doOptDc();
    };
    /**当窗口大小改变时，需要延时执行，瞬间执行会导致没有反应 */
    ScrollViewAssit.prototype.doOptDcDelay = function () {
        this.scheduleOnce(function () {
            this.doOptDc();
        }, 0);
    };
    ScrollViewAssit.prototype.doOptDc = function () {
        ScrollViewAssit_1.optDc(this.scrollView, this.caculatePosition);
    };
    /**
     * 优化 ScrollView Content 节点 DC，可以手动调用
     *
     * 具体为
     *
     * 1. 进入ScrollView可视区域是，回调对应 Content 子节点上挂载的 ScollViewPlusItem 组件的 onEnterScorllViewEvents 数组事件
     * 2. 退出ScrollView可视区域是，回调对应 Content 子节点上挂载的 ScollViewPlusItem 组件的 onExitScorllViewEvents 数组事件
     */
    ScrollViewAssit.optDc = function (scrollView, caculatePosition) {
        // 获取 ScrollView Node 的左下角坐标在世界坐标系中的坐标
        var svLeftBottomPoint = scrollView.node.parent.convertToWorldSpaceAR(cc.v2(scrollView.node.x - scrollView.node.anchorX * scrollView.node.width, scrollView.node.y - scrollView.node.anchorY * scrollView.node.height));
        // 求出 ScrollView 可视区域在世界坐标系中的矩形（碰撞盒）
        var svBBoxRect = cc.rect(svLeftBottomPoint.x, svLeftBottomPoint.y, scrollView.node.width, scrollView.node.height);
        // 遍历 ScrollView Content 内容节点的子节点，对每个子节点的包围盒做和 ScrollView 可视区域包围盒做碰撞判断
        scrollView.content.children.forEach(function (childNode) {
            // 没有绑定指定组件的子节点不处理
            var itemComponent = childNode.getComponent(ScrollViewEvent_1.default);
            if (itemComponent == null) {
                return;
            }
            // 如果相交了，那么就显示，否则就隐藏
            var childNodeBBox = childNode.getBoundingBoxToWorld();
            if (childNodeBBox.intersects(svBBoxRect)) {
                if (!itemComponent.isShowing) {
                    itemComponent.isShowing = true;
                    itemComponent.publishOnEnterScrollView();
                }
                if (caculatePosition) {
                    if (itemComponent.isShowing) {
                        itemComponent.publishOnPositionChange((childNodeBBox.x - (svBBoxRect.x - childNodeBBox.width / 2)) / (childNodeBBox.width + svBBoxRect.width), (childNodeBBox.y - (svBBoxRect.y - childNodeBBox.height / 2)) / (childNodeBBox.height + svBBoxRect.height));
                    }
                }
            }
            else {
                if (itemComponent.isShowing) {
                    itemComponent.isShowing = false;
                    itemComponent.publishOnExitScrollView();
                }
            }
        });
    };
    var ScrollViewAssit_1;
    __decorate([
        property({
            visible: false,
        })
    ], ScrollViewAssit.prototype, "scrollView", void 0);
    __decorate([
        property({
            tooltip: "是否计算在可视区域中Item的相对位置（可能会相对耗性能）"
        })
    ], ScrollViewAssit.prototype, "caculatePosition", void 0);
    ScrollViewAssit = ScrollViewAssit_1 = __decorate([
        ccclass
    ], ScrollViewAssit);
    return ScrollViewAssit;
}(cc.Component));
exports.default = ScrollViewAssit;

cc._RF.pop();